<template>
  <div>
    <van-nav-bar left-text="返回" left-arrow @click-left="onClickLeft" />
  </div>
  
  <div class="title">  注册艺龙账号  </div>
 
  <div class="login">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit" @click="submitForm"> 注册 </van-button>
      </div>
    </van-form>
    <van-popup v-model:show="show" class='pop'> {{qq}}</van-popup>
  </div>
</template>
<script setup>
import { ref,} from "vue";
import { useRouter } from "vue-router";
import $bus from "../config/eventBus";
import link from '../api/Links'
const show=ref(false)
 const showPopup = () => {
      show.value = true;
    };
const qq='注册成功'
const $router = useRouter();
const onClickLeft = () => {
  $router.push({ path: "/login" });
};
const username = ref("");
const password = ref("");
const onSubmit = (values) => {
  console.log("submit", values);
};
  $bus.emit("tabBarHide", false);


const time =()=>{
  setTimeout(function(){
     $router.push({ path: "/login" });
  },1500)
}

const submitForm=(val)=>{
  if(val){
    let data={
      name:username.value,
      pwd:password.value
    };
    console.log('data',data)
    link("/register", "POST", data).then((ok) => {
        console.log(ok,'ok');
         localStorage.setItem('username',data.name)       
        showPopup()
        time()
     });
     return clearTimeout(time)
  }else{
    console.log("error submit!");
      return false;
  }

}

</script>
<style>
.login {
  margin-top: 10%;
}
.title{
    text-align: left;
    font-size: 20px;
    margin-top:40px ;
}
.pop{
  width: 200px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  border-radius:8px;
}
</style>
